package 线程;

/**
 * @author:谢君臣
 * @Date:2021/9/15-14:02
 * @version:1.0
 * @function:
 */
public class Sleep和wait {
    public static void main(String[] args) {
        Object o = new Object();
//        System.out.println("-------------sleep----------");
//        new Thread(() -> {
//            synchronized (o) {
//                try {
//                    System.out.println("sleep");
//                    Thread.sleep(2000);
//                    System.out.println("replace lock");
//                } catch (InterruptedException e) {
//                    e.printStackTrace();
//                }
//            }
//        }).start();
//        try {
//            Thread.sleep(1000);
//        } catch (InterruptedException e) {
//            e.printStackTrace();
//        }
//        new Thread(() -> {
//            System.out.println("tryLock");
//            synchronized (o) {
//                System.out.println("getLock");
//                System.out.println("asfasdf");
//            }
//        }).start();

        System.out.println("-------------wait----------");
        new Thread(() -> {
            synchronized (o) {
                try {
                    System.out.println("sleep");
                    o.wait();
                    System.out.println("replace lock");
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }).start();
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        new Thread(() -> {
            System.out.println("tryLock");
            synchronized (o) {
                System.out.println("getLock");
                System.out.println("asfasdf");
                o.notifyAll();
            }
        }).start();

    }
}
